﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;


namespace FYPHP
{
   
    public partial class SearchMember : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);
  
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SqlCommand cmdMember;
            SqlDataReader dtrMember;
            conn.Open();

            if (txtIC.Text == "" && txtUserName.Text == "" && txtEmail.Text == "" && txtPosition.Text == "")
            {
                MultiView1.ActiveViewIndex = 1;
            }
            else
            {
                String query = "SELECT * FROM Users u WHERE u_gender IN('Male','Female')";

                if(txtIC.Text!="")
                    query += " AND u_ic =" + txtIC.Text;

                if (txtUserName.Text != "")
                    query += " AND username LIKE '" + txtUserName.Text + "'";

                if (txtEmail.Text != "")
                    query += " AND u_email ='" + txtEmail.Text + "'";

                if (txtPosition.Text != "")
                    query += " AND u_position ='" + txtPosition.Text + "'";


                cmdMember = new SqlCommand(query, conn);
                int num = Convert.ToInt32(cmdMember.ExecuteScalar());

                if (num != 0) //if member available
                {
                    dtrMember = cmdMember.ExecuteReader();

                    ItemList.DataSource = dtrMember;
                    ItemList.DataBind();
                    dtrMember.Close();

                    MultiView1.ActiveViewIndex = 0;
                }
                else //if member not available
                {
                    MultiView1.ActiveViewIndex = 1;
                }

                conn.Close();
            }


        }

        protected void ItemList_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "ViewMember")
            {
             
                int uid = Convert.ToInt32(e.CommandArgument.ToString());

                SqlCommand cmdUser;
                SqlDataReader dtrUser;
                conn.Open();

                cmdUser = new SqlCommand("Select * From Users where u_id = @Id", conn);
                cmdUser.Parameters.AddWithValue("@Id", uid);
                dtrUser = cmdUser.ExecuteReader();
                dtrUser.Read();

                String userName = dtrUser["username"].ToString();
                String gender = dtrUser["u_gender"].ToString();
                String age = dtrUser["u_age"].ToString();
                string addressBeforeSplit = dtrUser["u_address"].ToString();
              
                string[] lines = addressBeforeSplit.Split('|');

                string address = "";

                foreach (string line in lines)
                {
                    address += " " + line;
                }
                String email = dtrUser["u_email"].ToString();
                String rewardpoint = dtrUser["u_points"].ToString();
                String UserStatus = dtrUser["u_status"].ToString();
                String phoneNo = dtrUser["u_phone"].ToString();



                lblPID.Text = uid.ToString();
                lblName.Text = userName;
                lblGender.Text = gender;
                lblAge.Text = age;
                lblAddress.Text = address;
                lblEmail.Text = email;
                lblRewardPoints.Text = rewardpoint;
                lblPhoneNo.Text = phoneNo;

                dtrUser.Close();
                conn.Close();

                if (UserStatus.Equals("Activate"))
                {
                    btnMemberActivation.Text = "Deactivate " + txtUserName.Text + "'s Account";
                }
                else if (UserStatus.Equals("Deactivate") || UserStatus.Equals("DeactivateByAdmin"))
                {
                    btnMemberActivation.Text = "Activate " + txtUserName.Text + "'s Account";

                }
                MultiView2.ActiveViewIndex = 0;
               
            }
        }

        protected void btnEditProfile_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminEditMemberProfile.aspx?id=" + lblPID.Text.ToString());
        }

        protected void btnMemberActivation_Click(object sender, EventArgs e)
        {

            try
            {
                SqlCommand cmdUser;
                SqlDataReader dtrUser;
                conn.Open();



                cmdUser = new SqlCommand("Select * From Users where u_id=@UID ", conn);
                cmdUser.Parameters.AddWithValue("@UID",lblPID.Text);


                dtrUser = cmdUser.ExecuteReader();
                dtrUser.Read();

                String UserStatus = dtrUser["u_status"].ToString();

                dtrUser.Close();

                if (UserStatus.Equals("Activate"))
                {

                    SqlCommand cmdUpdate = new SqlCommand("Update Users SET u_status=@STATUS where u_id =@UID ", conn);
                    cmdUpdate.Parameters.AddWithValue("@STATUS", "DeactivateByAdmin");
                    cmdUpdate.Parameters.AddWithValue("@UID", lblPID.Text);
                    cmdUpdate.ExecuteNonQuery();
                    btnMemberActivation.Text = "Activate " + txtUserName.Text + "'s Account";
                    MessageBox("Account Deactivated");

                }
                else if (UserStatus.Equals("Deactivate") || UserStatus.Equals("DeactivateByAdmin"))
                {
                    SqlCommand cmdUpdate = new SqlCommand("Update Users SET u_status=@STATUS where u_id =@UID ", conn);
                    cmdUpdate.Parameters.AddWithValue("@STATUS", "Activate");
                    cmdUpdate.Parameters.AddWithValue("@UID", lblPID.Text);
                    cmdUpdate.ExecuteNonQuery();
                    btnMemberActivation.Text = "Deactivate " + txtUserName.Text + "'s Account";
                    MessageBox("Account Activated");

                }


                conn.Close();



            }
            catch (SqlException ex)
            {


            }
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

   
    }
}